package net.swa.dewei.web.action;

import java.util.ArrayList;
import java.util.List;
import net.swa.system.service.ICommonService;
import net.swa.system.util.annotation.Log;
import net.swa.system.web.action.AbstractBaseAction;
import net.swa.util.JsonResult;

public class CommonAction extends AbstractBaseAction {
	private static final long serialVersionUID = 523073008569799989L;
	private ICommonService commonService;
	private String type;
	private Long[] ids;
//	 private String[] attrNames;
//	private String orderBy;
//	private String orderType;
	
	
	/**
	 * 通用状态更新方法
	 * 
	 * @return
	 * @throws Exception
	 */
	@Log(description="更新状态")
	public String updateStatus() throws Exception {
		JsonResult<String> json = new JsonResult<String>();
		int status = Integer.parseInt(getRequestParameter("status"));
		commonService.commonUpdateStatus(type, ids,status);
		outJson(json);
		return null;
	}

	/**
	 * 通用删除记录方法
	 * 
	 * @return
	 * @throws Exception
	 */
	@Log(description="删除记录")
	public String delete() throws Exception {
		JsonResult<String> json = new JsonResult<String>();
		try {
			commonService.commonDelete(type, ids);
		} catch (Exception e) {
			e.printStackTrace();
			json.setSuccess(false);
			json.setMessage(e.getMessage());
		}
		outJson(json);
		return null;
	}
	@Log(description="删除记录")
	public String deleteHis() throws Exception {
		JsonResult<String> json = new JsonResult<String>();
		try {
			commonService.commonDelHisStatus(type,getRequestParameter("stateName"), ids,getRequestParameter("status"));
		} catch (Exception e) {
			e.printStackTrace();
			json.setSuccess(false);
			json.setMessage(e.getMessage());
		}
		outJson(json);
		return null;
	}
	/**
	 * 通用查询分页方法
	 * 
	 * @return
	 * @throws Exception
	 */
    public String search() throws Exception {
        currentPage=currentPage==0?1:currentPage;
        pageSize = pageSize == 0 ? 20 : pageSize;
        List<String> attrValues = null;
        List<String> operators = null;
        String[] attrNames = getRequest().getParameterValues("attrNames");
        if (attrNames == null) {
            attrNames = new String[] {};
        }
        List<String> paramName = new ArrayList<String>();
        attrValues = new ArrayList<String>();
        operators = new ArrayList<String>();

        for (int i = 0; i < attrNames.length; i++) {
            String[] value = getRequest().getParameterValues(attrNames[i]);
            if (!paramName.contains(attrNames[i])) {

                if (value != null) {
                    if (value.length > 1) {
                        String[] opers = getRequest().getParameterValues(
                                attrNames[i] + "_operator");
                        for (int j = 0; j < value.length; j++) {
                            attrValues.add(value[j]);
                            paramName.add(attrNames[i]);
                            operators.add(opers[j]);
                        }
                    } else if (value.length == 1) {
                        paramName.add(attrNames[i]);
                        attrValues.add(value[0]);
                        operators.add(getRequestParameter(attrNames[i]
                                + "_operator"));
                    } else {

                    }
                }

            }
        }
        Class<?> cLz = Class.forName(type);
        String[] searchAttr = new String[paramName.size()];
        paramName.toArray(searchAttr);
        String[] searchVal = new String[paramName.size()];
        attrValues.toArray(searchVal);
        String[] searchOper = new String[paramName.size()];
        operators.toArray(searchOper);

        JsonResult<?> json = commonService.search(searchAttr, searchVal,
                searchOper, cLz, currentPage, pageSize, orderBy, orderType);
        json.setCurrentPage(currentPage);
        outJson(json);
        return null;
    }


	// flow setter and getter
	public ICommonService getCommonService() {

		return commonService;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public String getOrderType() {
		return orderType;
	}

	public void setOrderType(String orderType) {
		this.orderType = orderType;
	}

}
